// $Id$
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.EnterpriseLibrary.Data;

namespace Northwind.DomainModel.EnterpriseLibrary
{

    /// <summary />
    partial class CustomerMapper
    {

        /// <summary />
        static CustomerMapper()
        {
            Resources.Sql.Culture=CultureInfo.InvariantCulture;
        }

        /// <summary />
        private DomainModelKeyedCollection<Customer> _FindAll(DataMapperCollection dataMappers)
        {
            DbCommand[] commands=new DbCommand[1];

            commands[0]=Database.GetSqlStringCommand(Resources.Sql.CustomerSelectAll);

            return Load(commands, dataMappers);
        }

        private Customer.DataTransfer _GetDataTransferObject(IList<IDataRecord> records)
        {
            Customer.DataTransfer data=new Customer.DataTransfer();

            return FillData(data, records[0]);
        }

        /// <summary />
        internal static Customer.DataTransfer FillData(Customer.DataTransfer data, IDataRecord record)
        {
            int ord=record.GetOrdinal("CustomerID");
            if (!record.IsDBNull(ord))
                data.CustomerID=record.GetString(ord);

            ord=record.GetOrdinal("CompanyName");
            if (!record.IsDBNull(ord))
                data.CompanyName=record.GetString(ord);

            ord=record.GetOrdinal("ContactName");
            if (!record.IsDBNull(ord))
                data.ContactName=record.GetString(ord);

            ord=record.GetOrdinal("ContactTitle");
            if (!record.IsDBNull(ord))
                data.ContactTitle=record.GetString(ord);

            ord=record.GetOrdinal("Address");
            if (!record.IsDBNull(ord))
                data.Address=record.GetString(ord);

            ord=record.GetOrdinal("City");
            if (!record.IsDBNull(ord))
                data.City=record.GetString(ord);

            ord=record.GetOrdinal("Region");
            if (!record.IsDBNull(ord))
                data.Region=record.GetString(ord);

            ord=record.GetOrdinal("PostalCode");
            if (!record.IsDBNull(ord))
                data.PostalCode=record.GetString(ord);

            ord=record.GetOrdinal("Country");
            if (!record.IsDBNull(ord))
                data.Country=record.GetString(ord);

            ord=record.GetOrdinal("Phone");
            if (!record.IsDBNull(ord))
                data.Phone=record.GetString(ord);

            ord=record.GetOrdinal("Fax");
            if (!record.IsDBNull(ord))
                data.Fax=record.GetString(ord);

            return data;
        }

        /// <summary />
        private static DbCommand _CreateSelectCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Customer.PrimaryKey key)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.CustomerSelect,
                    parameterBuilder.GetMarker("CustomerID", 1)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("CustomerID", 1),
                DbType.String,
                5,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                key.Value
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateDeleteCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Customer.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.CustomerDelete,
                    parameterBuilder.GetMarker("CustomerID", 1)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("CustomerID", 1),
                DbType.String,
                5,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                data.CustomerID
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateInsertCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Customer.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.CustomerInsert,
                    parameterBuilder.GetMarker("CustomerID", 1),
                    parameterBuilder.GetMarker("CompanyName", 2),
                    parameterBuilder.GetMarker("ContactName", 3),
                    parameterBuilder.GetMarker("ContactTitle", 4),
                    parameterBuilder.GetMarker("Address", 5),
                    parameterBuilder.GetMarker("City", 6),
                    parameterBuilder.GetMarker("Region", 7),
                    parameterBuilder.GetMarker("PostalCode", 8),
                    parameterBuilder.GetMarker("Country", 9),
                    parameterBuilder.GetMarker("Phone", 10),
                    parameterBuilder.GetMarker("Fax", 11)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("CustomerID", 1),
                DbType.String,
                5,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                data.CustomerID
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("CompanyName", 2),
                DbType.String,
                40,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                data.CompanyName
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ContactName", 3),
                DbType.String,
                30,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.ContactName ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ContactTitle", 4),
                DbType.String,
                30,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.ContactTitle ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Address", 5),
                DbType.String,
                60,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Address ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("City", 6),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.City ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Region", 7),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Region ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("PostalCode", 8),
                DbType.String,
                10,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.PostalCode ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Country", 9),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Country ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Phone", 10),
                DbType.String,
                24,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Phone ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Fax", 11),
                DbType.String,
                24,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Fax ?? (object)DBNull.Value
            );

            return command;
        }

        /// <summary />
        private static DbCommand _CreateUpdateCommand(Database database, Salamanca.DataAccess.Data.ParameterBuilder parameterBuilder, Customer.DataTransfer data)
        {
            DbCommand command=database.GetSqlStringCommand(
                string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.Sql.CustomerUpdate,
                    parameterBuilder.GetMarker("CompanyName", 1),
                    parameterBuilder.GetMarker("ContactName", 2),
                    parameterBuilder.GetMarker("ContactTitle", 3),
                    parameterBuilder.GetMarker("Address", 4),
                    parameterBuilder.GetMarker("City", 5),
                    parameterBuilder.GetMarker("Region", 6),
                    parameterBuilder.GetMarker("PostalCode", 7),
                    parameterBuilder.GetMarker("Country", 8),
                    parameterBuilder.GetMarker("Phone", 9),
                    parameterBuilder.GetMarker("Fax", 10),
                    parameterBuilder.GetMarker("CustomerID", 11)
                )
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("CompanyName", 1),
                DbType.String,
                40,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                data.CompanyName
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ContactName", 2),
                DbType.String,
                30,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.ContactName ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("ContactTitle", 3),
                DbType.String,
                30,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.ContactTitle ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Address", 4),
                DbType.String,
                60,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Address ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("City", 5),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.City ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Region", 6),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Region ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("PostalCode", 7),
                DbType.String,
                10,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.PostalCode ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Country", 8),
                DbType.String,
                15,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Country ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Phone", 9),
                DbType.String,
                24,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Phone ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("Fax", 10),
                DbType.String,
                24,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                (object)data.Fax ?? (object)DBNull.Value
            );

            database.AddParameter(
                command,
                parameterBuilder.GetName("CustomerID", 11),
                DbType.String,
                5,
                ParameterDirection.Input,
                false,
                0,
                0,
                string.Empty,
                DataRowVersion.Default,
                data.CustomerID
            );

            return command;
        }

        /// <summary>Gets the parameter builder instance.</summary>
        /// <value>The parameter builder instance.</value>
        protected Salamanca.DataAccess.Data.ParameterBuilder ParameterBuilder
        {
            get
            {
                return _ParameterBuilder;
            }
        }

        private Salamanca.DataAccess.Data.ParameterBuilder _ParameterBuilder;

    }

}
